// fifo.h
#ifndef FIFO_H
#define FIFO_H

#include <stdint.h>
#include <stdbool.h>

#define FIFO_BUFFER_SIZE 128  // 必须 <= 255

typedef struct {
    uint8_t buffer[FIFO_BUFFER_SIZE];
    uint8_t head;
    uint8_t tail;
    bool full;
} fifo_buffer_t;

void fifo_init(fifo_buffer_t *fifo);

bool fifo_is_empty(fifo_buffer_t *fifo);
bool fifo_is_full(fifo_buffer_t *fifo);
uint8_t fifo_available(fifo_buffer_t *fifo);
bool fifo_check_space(fifo_buffer_t *fifo, uint8_t required_len);
uint8_t fifo_peek(fifo_buffer_t *fifo, uint8_t *buf, uint8_t len);
void fifo_consume(fifo_buffer_t *fifo, uint8_t len);

bool fifo_write(fifo_buffer_t *fifo, uint8_t data);
bool fifo_read(fifo_buffer_t *fifo, uint8_t *data);

// 多字节操作接口
uint8_t fifo_write_buf(fifo_buffer_t *fifo, const uint8_t *data, uint8_t len);
uint8_t fifo_read_buf(fifo_buffer_t *fifo, uint8_t *buf, uint8_t bufsize);

#endif // FIFO_H